package com.gome.ocean.dao.model.mail;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailHepler {

    /**
     * 邮件消息格式转换
     * 
     * @param urlPars
     * @return
     */
    public static SimpleMailMessage convertMessage(HttpServletRequest req) {
        String receiver = req.getParameter("to");
        String title = req.getParameter("title");
        String content = req.getParameter("value");
        if (StringUtils.isEmpty(receiver) && StringUtils.isEmpty(title) && StringUtils.isEmpty(content)) {
            return null;
        }
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setText(content);
        mailMessage.setSubject(title);
        String to[] = receiver.split(",");
        mailMessage.setTo(to);
        mailMessage.setFrom("dataxmanager@gomeplus.com");
        return mailMessage;
    }

    /**
     * 邮件格式转换(html)
     * 
     * @param req
     * @param sender
     * @return
     */
    public static MimeMessage converHtmlMessage(HttpServletRequest req, JavaMailSender sender) {
        String receiver = req.getParameter("to");
        String title = req.getParameter("title");
        String content = req.getParameter("value");
        if (StringUtils.isEmpty(receiver) && StringUtils.isEmpty(title) && StringUtils.isEmpty(content)) {
            return null;
        }

        MimeMessage mailMessage = sender.createMimeMessage();
        // 设置utf-8或GBK编码，否则邮件会有乱码
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
            String to[] = receiver.split(",");
            messageHelper.setTo(to);
            messageHelper.setSubject(title);
            messageHelper.setFrom("dataxmanager@gomeplus.com");
            messageHelper.setText(content, true);
        } catch (MessagingException e) {
            e.printStackTrace();
            return null;
        }

        return mailMessage;
    }

    /**
     * Bean实体参数转换HTML发送邮件
     */
    public static MimeMessage BeanParamConverHtmlMessage(String receiver, String title, String content,
                                                         JavaMailSender sender) {
        if (StringUtils.isEmpty(receiver) && StringUtils.isEmpty(title) && StringUtils.isEmpty(content)) {
            return null;
        }
        MimeMessage mailMessage = sender.createMimeMessage();
        // 设置utf-8或GBK编码，否则邮件会有乱码
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
            String to[] = receiver.split(",");
            messageHelper.setTo(to);
            messageHelper.setSubject(title);
            messageHelper.setFrom("dataxmanager@gomeplus.com");
            messageHelper.setText(content, true);
        } catch (MessagingException e) {
            e.printStackTrace();
            return null;
        }

        return mailMessage;
    }
}
